package com.example.chinawenddingexpo.service;

import com.example.chinawenddingexpo.model.Coupon;
import com.example.chinawenddingexpo.model.UserCoupon;
import com.example.chinawenddingexpo.repository.CouponRepository;
import com.example.chinawenddingexpo.repository.UserCouponRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class CouponService {
    
    @Autowired
    private CouponRepository couponRepository;
    
    @Autowired
    private UserCouponRepository userCouponRepository;
    
    public List<Coupon> getAllAvailableCoupons() {
        return couponRepository.findAvailableCoupons(LocalDateTime.now());
    }
    
    public List<Coupon> getAvailableCouponsByExhibitor(Long exhibitorId) {
        return couponRepository.findAvailableCouponsByExhibitor(exhibitorId, LocalDateTime.now());
    }
    
    public Coupon getCouponById(Long id) {
        Optional<Coupon> coupon = couponRepository.findById(id);
        return coupon.orElse(null);
    }
    
    public Coupon getCouponByCode(String couponCode) {
        return couponRepository.findByCouponCode(couponCode);
    }
    
    public Coupon saveCoupon(Coupon coupon) {
        return couponRepository.save(coupon);
    }
    
    public boolean receiveCoupon(Long userId, Long couponId) {
        // 检查用户是否已经领取过
        UserCoupon existing = userCouponRepository.findByUserIdAndCouponId(userId, couponId);
        if (existing != null) {
            return false;
        }
        
        Coupon coupon = getCouponById(couponId);
        if (coupon == null || coupon.getStatus() != Coupon.CouponStatus.ACTIVE) {
            return false;
        }
        
        // 检查优惠券是否还有剩余
        if (coupon.getUsedQuantity() >= coupon.getTotalQuantity()) {
            return false;
        }
        
        // 检查是否在有效期内
        LocalDateTime now = LocalDateTime.now();
        if (now.isBefore(coupon.getStartTime()) || now.isAfter(coupon.getEndTime())) {
            return false;
        }
        
        // 创建用户优惠券关联
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUser(new com.example.chinawenddingexpo.model.User());
        userCoupon.getUser().setId(userId);
        userCoupon.setCoupon(coupon);
        userCouponRepository.save(userCoupon);
        
        // 更新优惠券使用数量
        coupon.setUsedQuantity(coupon.getUsedQuantity() + 1);
        if (coupon.getUsedQuantity() >= coupon.getTotalQuantity()) {
            coupon.setStatus(Coupon.CouponStatus.USED_UP);
        }
        couponRepository.save(coupon);
        
        return true;
    }
    
    public List<UserCoupon> getUserCoupons(Long userId) {
        return userCouponRepository.findByUserIdOrderByReceivedTimeDesc(userId);
    }
    
    public List<UserCoupon> getUserUnusedCoupons(Long userId) {
        return userCouponRepository.findUnusedCoupons(userId, LocalDateTime.now());
    }
    
    public boolean useCoupon(Long userId, Long couponId, String orderNo) {
        UserCoupon userCoupon = userCouponRepository.findByUserIdAndCouponId(userId, couponId);
        if (userCoupon == null || userCoupon.getStatus() != UserCoupon.UseStatus.UNUSED) {
            return false;
        }
        
        // 检查优惠券是否过期
        if (userCoupon.getCoupon().getEndTime().isBefore(LocalDateTime.now())) {
            userCoupon.setStatus(UserCoupon.UseStatus.EXPIRED);
            userCouponRepository.save(userCoupon);
            return false;
        }
        
        userCoupon.setStatus(UserCoupon.UseStatus.USED);
        userCoupon.setOrderNo(orderNo);
        userCoupon.setUsedTime(LocalDateTime.now());
        userCouponRepository.save(userCoupon);
        
        return true;
    }
}